package com.atguigu.gmall.mq.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.service.RabbitService;
import com.atguigu.gmall.mq.config.DeadLetterMqConfig;
import com.atguigu.gmall.mq.config.DelayedMqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/25 9:34
 * @Version 1.0
 */
@RestController
@RequestMapping("mq")
public class IndexController {

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送普通消息
     * @return
     */
    @GetMapping("sendConfirm")
    public Result sendMsg(){
        //  发生消息：
        rabbitService.sendMsg("exchange.confirm","routing.confirm666","死鬼，你怎么才回来.");
        return Result.ok();
    }

    //  发送消息:
    @GetMapping("sendDeadLetterMsg")
    public Result sendDeadLetterMsg(){
        //  设置一个时间：
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"死鬼，你快醒醒吧.");
        System.out.println(simpleDateFormat.format(new Date()));
        //  默认返回.
        return Result.ok();
    }

    //  发送消息:
    @GetMapping("sendDelayMsg")
    public Result sendDelayMsg(){
        //  设置一个时间：
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  rabbitService.sendMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"死鬼，你快醒醒吧.");
        //        this.rabbitTemplate.convertAndSend(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,"赶紧下课.",
        //                (msg)->{
        //                    //  设置延迟时间10s
        //                    msg.getMessageProperties().setDelay(10000);
        //                    System.out.println(simpleDateFormat.format(new Date()));
        //                    return msg;
        //                });
        //  发送延迟消息.
        //  rabbitService.sendDelayMsg(DeadLetterMqConfig.exchange_dead,DeadLetterMqConfig.routing_dead_1,"死鬼，你快醒醒吧.",10);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("messageId", UUID.randomUUID().toString());
        jsonObject.put("message", "atguigu");
        rabbitService.sendDelayMsg(DelayedMqConfig.exchange_delay,DelayedMqConfig.routing_delay,jsonObject,10);
        System.out.println(simpleDateFormat.format(new Date()));
        //  默认返回.
        return Result.ok();
    }
}
